from basepage import BasePage
from .. import widgets

class Setup(BasePage):
    page_name = 'Setup'
    def __init__(self, **kwargs):
        super(Setup, self).__init__(**kwargs)
        bounds = [self.x_offset, self.y_offset, 1., .1]
        self.build_topwidget('Label', name='Setup', bounds=bounds)
        bounds = [.1, .2, .4, .5]
        self.macros = self.topwidget.add_widget(Macros, 
                                                MainController=self.MainController, 
                                                bounds=bounds)
        self.topwidget.refresh_interface()
        
class Macros(widgets.Label):
    def __init__(self, **kwargs):
        self.MainController = kwargs.get('MainController')
        kwargs['name'] = 'Macros'
        super(Macros, self).__init__(**kwargs)
        macrodict = self.MainController.EffectsSystem.Macros
        self.btns = {}
        btn_h = self.height / len(macrodict)
        bkwargs = dict(x=self.x, width=self.width, height=btn_h)
        for i, macro in enumerate(macrodict.values()):
            bkwargs.update(dict(y=self.y + (btn_h * i), macro=macro))
            btn = self.add_widget(MacroBtn, **bkwargs)
            self.btns[macro.name] = btn
        
        
class MacroBtn(widgets.Button):
    def __init__(self, **kwargs):
        self.macro = kwargs.get('macro')
        kwargs['name'] = self.macro.name
        kwargs['label'] = self.macro.name
        super(MacroBtn, self).__init__(**kwargs)
        self.Property = (self.macro, 'running')
        self.bind(clicked=self.on_clicked)
    def on_clicked(self, **kwargs):
        self.LOG.info('running macro: ', self.name)
        self.macro.run()
